package com.ihcil.forecast.demo.controller;

import com.ihcil.forecast.demo.pojo.User;
import com.ihcil.forecast.demo.service.UserService;
import com.ihcil.forecast.demo.vo.LoginVo;
import com.ihcil.forecast.demo.vo.RegistVo;
import com.ihcil.forecast.demo.vo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * Create by lotus on 2021/8/13 21:43
 */

@Controller
@RequestMapping("/login")
@Slf4j // 打印日志
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

    // 跳转登录页面
    @RequestMapping(value = "/toLogin", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String tologin(Model model, HttpServletRequest request, HttpServletResponse response) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // Redis中直接获取页面，如果不为空，直接返回页面
        String html = (String) valueOperations.get("loginPage");
        if(!StringUtils.isEmpty(html)) {
            return html;
        }

        // 如果为空，手动渲染，存入Redis并返回
        // model中存放的是需要返回给前端的数据
        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(),
                model.asMap());

        // template存放的是返回给Thymeleaf渲染的字符串，这里我们需要返回到`login.html`页面，所以给一个`login`就能把渲染好
        // 的页面（字符串）返回
        html = thymeleafViewResolver.getTemplateEngine().process("login", webContext);

        if (!StringUtils.isEmpty(html)) {
            // 渲染成功
            valueOperations.set("loginPage", html, 60, TimeUnit.MINUTES);
        }
        return html;

//        return "login";
    }

    // 跳转注册页面
    @RequestMapping("/toRegist")
    public String toRegist() {
        return "regist";
    }

    // 登录功能
    @RequestMapping("/doLogin")
    @ResponseBody
    public RespBean doLogin(@Valid LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
//        log.info("{}", loginVo);
        return userService.doLogin(loginVo, request, response);
    }

    // 注册功能
    @PostMapping("/doRegist")
    @ResponseBody
    public RespBean regist(@Valid RegistVo registVo) {
        log.info("这是注册控制器");

        return userService.regist(registVo);
    }

}
